//
//  SLBottomAlertBaseView.m
//  shunlianPower2
//
//  Created by 陈镇 on 2020/6/23.
//  Copyright © 2020 顺联网络. All rights reserved.
//

#import "SLBottomAlertBaseView.h"

@implementation SLBottomAlertBaseView{
    SLTapView *_mainView;
    UIView *_bgView;
    CGFloat _contentRound;
    SLContentRoundType _roundType;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
         [self sl_createUI];
        
        self.contentBackgroundColor = UIColorWhite;
        self.contentFrame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_WIDTH);
        [self setContentRound:20 type:SLContentRoundTypeTop];
        [self sl_addDisMissTapAnimationView:_mainView];
       
    }
    return self;
}

-(void)sl_createUI{
    _mainView = [[SLTapView alloc] initWithFrame:self.contentFrame];
    [self addSubview:_mainView];
    [_mainView sl_addNoResponseTap];
    
    _bgView = [[UIView alloc] initWithFrame:_mainView.bounds];
    [_mainView addSubview:_bgView];
}

-(void)setContentRound:(CGFloat)contentRound type:(SLContentRoundType)type{
    _contentRound = contentRound;
    _roundType = type;
    if (contentRound <= 0) {
        return;
    }
    
    UIRectCorner corner = UIRectCornerTopLeft | UIRectCornerTopRight;
    switch (type) {
        case SLContentRoundTypeTop:
            corner = UIRectCornerTopLeft | UIRectCornerTopRight;
            break;
        case SLContentRoundTypeBottom:
            corner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
        break;
        case SLContentRoundTypeAll:
            corner = UIRectCornerAllCorners;
        break;
            
        default:
            break;
    }
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_bgView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(contentRound, contentRound)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _bgView.bounds;
    maskLayer.path = maskPath.CGPath;
    _bgView.layer.mask = maskLayer;

}

                 
-(void)setContentBackgroundColor:(UIColor *)contentBackgroundColor{
    _contentBackgroundColor = contentBackgroundColor;
    _bgView.backgroundColor = contentBackgroundColor;
}



-(void)setContentFrame:(CGRect)contentFrame{
    _contentFrame = contentFrame;
    _mainView.frame = contentFrame;
    _bgView.frame = _mainView.bounds;
    [self setContentRound:_contentRound type:_roundType];
}
-(void)setAlertBottomMargin:(CGFloat)alertBottomMargin{
    _alertBottomMargin = alertBottomMargin;
    self.bottomMargin = alertBottomMargin;
}
-(UIView *)contentView{
    return _mainView;
}
-(UIView *)roundcontentView{
    return _bgView;
}
+(instancetype)showViewToSuperView:(UIView *)superView{
    SLBottomAlertBaseView *view = [[self alloc] initWithFrame:SLWindow.bounds];
    [superView addSubview:view];
    [view sl_showBottomSubView:view.contentView];
    
    return view;
}

-(void)showView{
    [self sl_showBottomSubView:_mainView];
}
-(void)disMissView{
    [self sl_disMissView];
    [self removeFromSuperview];
}
@end
